<template>
	<view>
		<!-- 导航栏 -->
		<cu-custom bgColor="bg-gradual-red" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">修改密码</block>
		</cu-custom>
		<form>
			<view class="cu-form-group  margin-top-sm">
				<view class="title text-bold">旧的密码</view>
				<input class="" type="password" placeholder="请输入旧密码" name="password" v-model="oldPwd"></input>
			</view>
			<view class="cu-form-group ">
				<view class="title text-bold">新的密码</view>
				<input class="" type="password" placeholder="请输入新密码" name="password" v-model="newPwd"></input>
			</view>
			<view class="cu-form-group ">
				<view class="title text-bold">密码确认</view>
				<input class="" type="password" placeholder="请再次输入新密码" name="password" v-model="newPwd2"></input>
			</view>
			<view class="flex margin-top padding-lr">
				<button class="cu-btn shadow flex-sub bg-purple padding-sm margin-xs radius" @tap="changPwd">提交</button>
				<button class="cu-btn shadow flex-sub bg-grey padding-sm margin-xs radius" @tap="go_back">返回</button>
			</view>
		</form>
		
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	export default {
		computed: mapState(['userName','pwd','server']),
		data() {
			return {
				oldPwd: '',
				newPwd: '',
				newPwd2: ''
			}
		},
		methods: {
			...mapMutations(['login','setName','setDeptName','setPwd','logout']),
			changPwd(){
				if(this.oldPwd.length==0){
					uni.showToast({
						title: '旧密码不能为空',
						icon: 'none'
					});
					return;
				}
				/* if(this.newPwd.length==0){
					uni.showToast({
						title: '新密码不能为空',
						icon: 'none'
					});
					return;
				} */
				var ragx=/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{8,10}/;
				if(!ragx.test(this.newPwd)){
					uni.showToast({
						title: '新密码至少8个字符，至少1个大写字母，1个小写字母，1个数字，1个特殊字符',
						icon: 'none'
					});
					return;
				}
				if(this.newPwd!=this.newPwd2){
					uni.showToast({
						title: '两次输入的新密码不相同,请重新输入',
						icon: 'none'
					});
					this.newPwd='';
					this.newPwd2='';
					return;
				}
				if(this.oldPwd!=this.pwd){
					uni.showToast({
						title: '旧密码不正确，请重新输入',
						icon: 'none'
					});
					this.oldPwd='';
					return;
				}
				uni.request({
					url: this.server.url + 'app/changePwd',
					method: 'GET',
					sslVerify: false,
					data: {userName:this.userName ,oldPwd:this.oldPwd,newPwd:this.newPwd},
					success: res => {
						var result=res.data;
						if(result.success){
							uni.showModal({
								title: '提示',
								content: '密码修改成功，点击确定后重新登录',
								showCancel: false,
								success: res => {
									uni.redirectTo({
										url: 'login'
									});
								}
							});
						}else{
							uni.showToast({
								title: result.msg,
								icon: 'none'
							});
						}
					},
					fail: () => {
						uni.showToast({
							title: '网络连接失败，请检查你的网络...',
							icon: 'none'
						});
					}
				});
				
			},
			go_back(){
				uni.navigateBack();
			}
		}
	}
</script>

<style>

</style>
